package com.gitee.dbswitch.admin.model.request;

import com.gitee.dbswitch.admin.entity.APIAssignmentConfigEntity;
import com.gitee.dbswitch.admin.entity.AssignmentConfigEntity;
import com.gitee.dbswitch.admin.type.IncludeExcludeEnum;
import com.gitee.dbswitch.common.entity.PatternMapper;
import com.gitee.dbswitch.common.type.CaseConvertEnum;
import com.gitee.dbswitch.common.type.ProductTableEnum;
import com.gitee.dbswitch.common.type.SyncOptionEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class APITaskBaseRequest {
    @NoArgsConstructor
    @Data
    public static class APITaskConfig {

        private String apiName;
        private String apiURL;
        private String requestMethod;
        private String requestTime;
        private String targetTable;
        private String synchronizeType;
        private String apiJsonRoot;
        private String apiJsonPosition;
        private List<Map<String,String>> apiParams;
        private List<Map<String,String>> apiHeaders;
        private String apiBody;
        private Long targetConnectionId;
        private String targetSchema;
        private CaseConvertEnum tableNameCase;
        private CaseConvertEnum columnNameCase;
        private List<PatternMapper> tableNameMapper;
        private List<PatternMapper> columnNameMapper;
        private Boolean targetDropTable;
        private Boolean targetOnlyCreate;
        private Boolean targetAutoIncrement;
        private SyncOptionEnum targetSyncOption;
        private String beforeSqlScripts;
        private String afterSqlScripts;
        private Integer batchSize;
        private Integer channelSize;
    }
    protected APIAssignmentConfigEntity toAPIConfig(final Long assignmentId, final APITaskConfig config) {
        APIAssignmentConfigEntity apiAssignmentConfigEntity = new APIAssignmentConfigEntity();
        apiAssignmentConfigEntity.setAssignmentId(assignmentId);
        apiAssignmentConfigEntity.setApiName(config.getApiName());
        apiAssignmentConfigEntity.setApiURL(config.getApiURL());
        apiAssignmentConfigEntity.setRequestMethod(config.getRequestMethod());
        apiAssignmentConfigEntity.setRequestTime(config.getRequestTime());
        apiAssignmentConfigEntity.setTargetConnectionId(config.getTargetConnectionId());
        apiAssignmentConfigEntity.setTargetTable(config.getTargetTable());
        apiAssignmentConfigEntity.setSynchronizeType(config.getSynchronizeType());
        apiAssignmentConfigEntity.setApiJsonRoot(config.getApiJsonRoot());
        apiAssignmentConfigEntity.setApiJsonPosition(config.getApiJsonPosition());
        apiAssignmentConfigEntity.setApiParams(config.getApiParams());
        apiAssignmentConfigEntity.setApiHeaders(config.getApiHeaders());
        apiAssignmentConfigEntity.setApiBody(config.getApiBody());
        apiAssignmentConfigEntity.setTargetSchema(config.getTargetSchema());
        apiAssignmentConfigEntity.setTableNameCase(config.getTableNameCase());
        apiAssignmentConfigEntity.setColumnNameCase(config.getColumnNameCase());
        apiAssignmentConfigEntity.setTableNameMap(config.getTableNameMapper());
        apiAssignmentConfigEntity.setColumnNameMap(config.getColumnNameMapper());
        apiAssignmentConfigEntity.setTargetDropTable(config.getTargetDropTable());
        apiAssignmentConfigEntity.setTargetOnlyCreate(config.getTargetOnlyCreate());
        apiAssignmentConfigEntity.setTargetAutoIncrement(config.getTargetAutoIncrement());
        apiAssignmentConfigEntity.setBeforeSqlScripts(getTrimValueOrNull(config.getBeforeSqlScripts()));
        apiAssignmentConfigEntity.setAfterSqlScripts(getTrimValueOrNull(config.getAfterSqlScripts()));
        apiAssignmentConfigEntity.setTargetSyncOption(config.getTargetSyncOption());
        apiAssignmentConfigEntity.setBatchSize(getValueOrDefault(config.getBatchSize(), 10000));
        apiAssignmentConfigEntity.setChannelSize(getValueOrDefault(config.getChannelSize(), 100));
        apiAssignmentConfigEntity.setFirstFlag(Boolean.FALSE);

        return apiAssignmentConfigEntity;
    }
    protected int getValueOrDefault(Integer value, int defaultValue) {
        return Objects.nonNull(value) ? value : defaultValue;
    }

    protected String getTrimValueOrNull(String value) {
        return Objects.nonNull(value) ? value.trim() : null;
    }
}
